package org.nuist.service.virtualmachine;

import org.nuist.pojo.VirtualMachine;
import org.nuist.repository.VirtualMachineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VirtualMachineServiceImpl implements VirtualMachineService{

    @Autowired
    VirtualMachineRepository virtualMachineRepository;

    @Override
    public void save(VirtualMachine vm) {
        virtualMachineRepository.save(vm);
    }

    @Override
    public void delete(VirtualMachine vm) {
        virtualMachineRepository.delete(vm);
    }

    @Override
    public List<VirtualMachine> findAll() {
        return virtualMachineRepository.findAll();
    }

    @Override
    public VirtualMachine findVirtualMachineById(Integer id) {
        return virtualMachineRepository.findVirtualMachineById(id);
    }

    @Override
    public void modifyVirtualMachine(VirtualMachine vm) {
        int vmId=vm.getVirtualMachineId();
        VirtualMachine oldVm=virtualMachineRepository.findVirtualMachineById(vmId);
        oldVm.setIp(vm.getIp());
        oldVm.setPassword(vm.getPassword());
        oldVm.setPort(vm.getPort());
        oldVm.setUserName(vm.getUserName());
        oldVm.setOccupied(vm.isOccupied());
        oldVm.setBootDisk(vm.getBootDisk());
        oldVm.setMem(vm.getMem());
        oldVm.setStatus(vm.getStatus());
        virtualMachineRepository.save(oldVm);
    }

    @Override
    public List<VirtualMachine> findFreeVirtualMachine() {
        return virtualMachineRepository.findFreeVirtualMachine();
    }

    @Override
    public VirtualMachine findVirtualMachineByPort(String port) {
        return virtualMachineRepository.findVirtualMachineByPort(port);
    }
}
